package com.yaoxiaowen.project2.test.recycler_view;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.view.ViewGroup;

import com.yaoxiaowen.project2.util.log.debug.LogInterface;
import com.yaoxiaowen.project2.util.log.debug.LogTool;

/**
 * Created by Administrator on 2016/12/31.
 */

public abstract class HeaderFooterCommonAdapter<T> extends CommonAdapter<T> {

    //item 类型
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_NORMAL = 1;
    public static final int ITEM_TYPE_FOOTER = 2;

    private int mHeaderCount = 0;
//    private int mHeaderResId;
    private View mHeadView;
    private int mFooterCount = 0;
//    private int mFooterResId;
    private View mFootView;


    protected OnHeadFootItemClickListener mHeadFootListener = null;

    public static final String TAG = "HeaderFooterCommonAdapter";
    private static final LogInterface mLog = LogTool.getLogType();
    private static final boolean DEBUG = true;

    public HeaderFooterCommonAdapter(Context mContenxt, @LayoutRes int mLayoutResId) {
        super(mContenxt, mLayoutResId);
    }

    public void setHeadFootListener(OnHeadFootItemClickListener headFootListener){
        mHeadFootListener = headFootListener;
    }

//    public void setHeaderRes(@LayoutRes int headerResId){
//        mHeaderResId = headerResId;
//        mHeaderCount = 1;
//    }
//
//    public void setFooterRes(@LayoutRes int footerResId){
//        mFooterResId = footerResId;
//        mFooterCount = 1;
//    }

    public void setHeadView(View headView){
        mHeadView = headView;
        mHeaderCount = 1;
    }

    public void setFootView(View footView){
        mFootView = footView;
        mFooterCount++;
    }


    @Override
    public int getItemViewType(int position) {
//        int dataCount = super.getItemCount();
//        if (mHeaderCount>0 && position<mHeaderCount){
//            return ITEM_TYPE_HEADER;
//        }else if (mFooterCount>0 && position>=(mHeaderCount+dataCount)){
//            return ITEM_TYPE_FOOTER;
//        }else {
//            return ITEM_TYPE_NORMAL;
//        }
        if (mHeaderCount==0 && mFooterCount==0){
            return ITEM_TYPE_NORMAL;
        }
        if (position == 0){
            return ITEM_TYPE_HEADER;
        }
        if (position == getItemCount()-1){
            return ITEM_TYPE_FOOTER;
        }
        return ITEM_TYPE_NORMAL;
    }

    @Override
    protected boolean isEnabled(int viewType) {
        if (viewType!=ITEM_TYPE_NORMAL){
            return false;
        }
        return super.isEnabled(viewType);
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        if (viewType == ITEM_TYPE_HEADER){
//            return CommonViewHolder.get(mContenxt, null, parent, mHeaderResId, -1);
//        }
//
//        if (viewType == ITEM_TYPE_FOOTER){
//            return CommonViewHolder.get(mContenxt, null, parent, mFooterResId, -1);
//        }
//
//        StringBuilder sb = new StringBuilder();
//        sb.append("onCreateViewHolder()     ");
//
//        int resId = mLayoutResId;
//        switch (viewType){
//            case ITEM_TYPE_HEADER:
//                resId = mHeaderResId;
//                sb.append("  Header");
//                break;
//            case ITEM_TYPE_FOOTER:
//                resId = mFooterResId;
//                sb.append("  footer");
//                break;
//            case ITEM_TYPE_NORMAL:
//                resId = mLayoutResId;
//                sb.append("  normal");
//                break;
//            default:
//        }


        if (mHeadView!=null && viewType==ITEM_TYPE_HEADER){
            return new CommonViewHolder(mContenxt, mHeadView);
        }
        if (mFootView!=null && viewType==ITEM_TYPE_FOOTER){
            return new CommonViewHolder(mContenxt, mFootView);
        }

        CommonViewHolder commonHolder = CommonViewHolder.get(mContenxt, null, parent, mLayoutResId, -1);
        this.setListener(parent, commonHolder, viewType);
        return commonHolder;
    }

    @Override
    protected void setListener(final ViewGroup parent, final CommonViewHolder viewHolder, final int viewType) {

        viewHolder.getContentView().setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                switch (viewType){
                    case ITEM_TYPE_HEADER:
                        if (mHeadFootListener != null){
                            mHeadFootListener.onHeaderItemClick(viewHolder.getContentView(), viewHolder);
                        }
                        break;
                    case ITEM_TYPE_FOOTER:
                        if (mHeadFootListener != null){
                            mHeadFootListener.onFooterItemClick(viewHolder.getContentView(), viewHolder);
                        }
                        break;
                    case ITEM_TYPE_NORMAL:
                        if (mOnItemClickListner != null){
                            int pos = getPosition(viewHolder) - mHeaderCount;
                            mOnItemClickListner.onItemClick(parent, v, mDatas.get(pos), pos);
                        }
                        break;
                }
            }
        });

        viewHolder.getContentView().setOnLongClickListener(new View.OnLongClickListener(){
            @Override
            public boolean onLongClick(View v) {

                switch (viewType){
                    case ITEM_TYPE_HEADER:
                        if (mHeadFootListener != null){
                            mHeadFootListener.onHeaderItemLongClick(viewHolder.getContentView(), viewHolder);
                        }
                        break;
                    case ITEM_TYPE_FOOTER:
                        if (mHeadFootListener != null){
                            mHeadFootListener.onFooterItemLongClick(viewHolder.getContentView(), viewHolder);
                        }
                        break;
                    case ITEM_TYPE_NORMAL:
                        if (mOnItemClickListner != null){
                            int pos = getPosition(viewHolder) - mHeaderCount;
                            return mOnItemClickListner.onItemLongClick(parent, v, mDatas.get(pos), pos);
                        }
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void onBindViewHolder(CommonViewHolder holder, int position) {


        int type = getItemViewType(position);



        int itemType = getItemViewType(position);
        if (itemType == ITEM_TYPE_NORMAL){
            super.onBindViewHolder(holder, position-mHeaderCount);
        }else {
            convertHeadOrFoot(holder, itemType == ITEM_TYPE_HEADER);
        }
    }

    public abstract void convertHeadOrFoot(CommonViewHolder holder, boolean isHead);

    @Override
    public int getItemCount() {
        return mHeaderCount + super.getItemCount() + mFooterCount;
    }
}
